<?php

namespace App\Http\Controllers\group;

use App\Http\Controllers\Controller;
use App\Models\Activity;
use App\Models\Goods;
use App\Models\Member;
use App\Models\Order;
use Illuminate\Http\Request;
use Illuminate\Pagination\Paginator;

class GroupController extends Controller
{
      /**
       * user高传想
       * get
       * 活动列表
       * {"data":{"current_page":1,"data":[{"id":1,"user_id":1,"activity_name":"\u8305\u53f0\u75af\u62a2\u4e86","activity_title":"\u8305\u53f0\u9152","limit_num":100,"status":0,"start_time":"2023-04-21 19:30:05","end_time":"2023-05-21 19:30:09","is_on_sale":1,"created_at":"2023-04-21T19:30:47.000000Z","updated_at":"2023-04-21T19:30:47.000000Z","deleted_at":null}],"first_page_url":"http:\/\/www.qiwei.com\/activity?page=1","from":1,"last_page":1,"last_page_url":"http:\/\/www.qiwei.com\/activity?page=1","links":[{"url":null,"label":"&laquo; Previous","active":false},{"url":"http:\/\/www.qiwei.com\/activity?page=1","label":"1","active":true},{"url":null,"label":"Next &raquo;","active":false}],"next_page_url":null,"path":"http:\/\/www.qiwei.com\/activity","per_page":1,"prev_page_url":null,"to":1,"total":1}}
       */
        public function activity(Request $request)
        {
            Paginator::useBootstrap();
            $activity_name=$request->get('activity_name');
            $data=Activity::where('activity_name','like',"%$activity_name%")->paginate(5);
           return view('group/activityList',compact('data','activity_name'));
        }
    /**
     * user高传想
     * get
     * 添加页面
     */
      public function ArtivityCreate()
      {
          return view('group/activityCreate');
      }
    /**
     * user高传想
     * post
     * 执行添加
     *{"data":{"activity_name":"\u6700\u65b0\u82f9\u679c\u624b\u673a\u4e0a\u67b6\u4e86","activity_title":"\u82f9\u679c\u624b\u673a","status":"0","is_on_sale":"1","start_time":"2023-04-21T19:55","end_time":"2023-05-21T19:55","updated_at":"2023-04-21T11:55:11.000000Z","created_at":"2023-04-21T11:55:11.000000Z","id":3}}
     */
    public function activitySave(Request $request)
    {
        $params=$request->except(['_token']);
        $res=Activity::create($params);
        if ($res)
        {
            return redirect(route('activity'));
        }
         return  redirect(route('ArtivityCreate'));

    }
    /**
     * user高传想
     * get
     * 活动删除
     *{"data":1}
     */
     public function activityDel($id){
         $res=Activity::destroy($id);
         if ($res) {
             return redirect(route('activity'));
         }
     }

    /**
     * user高传想
     * get
     * 判断活动是否开始
     *
     */

    public function activityStatus(Request $request){
        $activityId=$request->get('id');
        $user_id=$request->get('user_id');
        $activityTime=Activity::where('id',1)->where('user_id',1)->first();
        $startTime=$activityTime['start_time'];
        $endTime=$activityTime['end_time'];
        $nowTime=date('Y-m-d H:i:s');
//        判断当前时间是否大于开始时间
        if (strtotime($nowTime)-strtotime($startTime)>0&&strtotime($nowTime)-strtotime($endTime)<0&&$activityTime['status']==1)
        {
            return ['code'=>'200','msg'=>'活动开始','data'=>$startTime];
        }elseif (strtotime($nowTime)-strtotime($startTime)<0&&$activityTime['status']==0)
        {
            return ['code'=>'200','msg'=>'活动未开始','data'=>null];
        }else
            {
            return ['code'=>'200','msg'=>'活动已结束','data'=>null];
        }
    }

    /**
     * user高传想
     * get
     * 发起拼团
     *
     */
     public function group(Request $request)
     {
          $goods_id=$request->get('goods_id');
          $user_id=$request->get('user_id');
//          查询活动是否开始
           $getActivityStatus=$this->activityStatus();
         $activity=Activity::where('id',1)->where('user_id',1)->value('status');
          if ($activity==0){
//              活动商品页面按钮显示活动还有多久开始时间
              return ['code'=>200,'msg'=>'活动未开始','data'=>null];
          }elseif ($activity==2){
              //活动商品页面按钮显示活动已结束
              return ['code'=>200,'msg'=>'活动已结束','data'=>null];
          }else{
              $stock=Goods::where('id',$goods_id)->value('goods_number');
              if ($stock==0){
//                  首页按钮显示已抢光
                  return ['code'=>200,'msg'=>'库存已抢光','data'=>null];
              }

              $teamLeader=Member::where('id',$user_id)->value('pt_num');

              $key='leader'.$user_id;
              if ($teamLeader==1)
              {
              }
//              判断订单表拼团的商品是否已经存在，并且是待支付状态；
              $groupOrder =Order::where('goods_id', $goods_id)->where('status', 0)->first();
              if ($groupOrder){
//                  实例化拼团成员模型
                  $member = new Member();
//                  更新拼团成员表里的订单id和用户id
                  $member->order_id=$groupOrder->id;
                  $member->user_id=$groupOrder->user_id;
                  $member->save();
                  return[
                      'code' => 0,
                      'msg' => '参加拼团成功',
                      'data' => [
                          'order_id' => $groupOrder->id
                      ]
                  ];

              }
          }
     }
}
